<?php
class Browser { 
    /** 
    Figure out what browser is used, its version and the platform it is 
    running on. 

    The following code was ported in part from JQuery v1.3.1 
    */ 
    public static function detect() { 
        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); 
    

        // Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari. 
        if (preg_match('/ubrowser/i', $userAgent)) { 
            $name = 'ubrowser'; 
        } 
        elseif (preg_match('/ucbrowser/i', $userAgent)) { 
            $name = 'ucbrowser'; 
        }
        elseif (preg_match('/(mqqbrowser)+|(qqbrowser)+/i', $userAgent, $matches)) { 
            $name = $matches[0]; 
            if ($name == "mqqbrowser") {  
                //匹配是否是qqapp内置qq浏览器      
                if (preg_match('/(qq\/)+.*/i', $userAgent, $mqqmatches)){
                    if (isset($mqqmatches['1']) && $mqqmatches['1'] != "") {
                        $name = 'mqqInternalBrowser';
                    }
                } elseif (preg_match('/micromessenger/i', $userAgent)) {  
                    //匹配是否是微信内置的qq浏览器 .草泥马微信     
                    $name = 'wechatbrowser';
                }                         
            }   
        }
        elseif (preg_match('/ qq/', $userAgent)) {
            $name = 'mqqInternalBrowser';
        }
        elseif (preg_match('/micromessenger/i', $userAgent)) {  
            $name = 'wechatbrowser';
        }
        elseif (preg_match('/vivobrowser/i', $userAgent)) {
            // vivo浏览器标识
            $name = 'vivobrowser';
        }
        else { 
            $name = 'unrecognized'; 
        } 


        // What version? 
        if (preg_match('/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches)) { 
            $version = $matches[1]; 
        } 
        else { 
            $version = 'unknown'; 
        } 

        // Running on what platform? 
        if (preg_match('/android/i', $userAgent)) { 
            $platform = 'android'; 
        } 
        elseif (preg_match('/iphone/i', $userAgent)) { 
            $platform = 'iphone'; 
        } 
        else { 
            $platform = 'unrecognized'; 
        } 

        return array( 
            'name'      => $name, 
            'version'   => $version, 
            'platform'  => $platform, 
            'userAgent' => $userAgent 
        ); 
    } 
} 
